package com.songcha.module_main.ui.fragment.tqa

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.webkit.JavascriptInterface
import android.webkit.ValueCallback
import android.webkit.WebView
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.ui.view.HeaderBarView
import com.songcha.library_common.util.DeviceUtil
import com.songcha.library_common.util.PackageUtil
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.StatusBarUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.library_webview.ui.fragment.WebViewFragment
import org.json.JSONObject

class TQAFragment:WebViewFragment() {


    init {
        TAG = TQAFragment::class.java.simpleName
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        if(mWebView == null) return
        mWebView!!.addJavascriptInterface(JSObject(requireActivity(),mWebView!!,mHbvTitle!!),"Android")
    }

    class JSObject(val activity: Activity, val webView: WebView,val hbv:HeaderBarView): Object() {
        private var mCanGoBack = true

        @JavascriptInterface
        fun getLoginInfo(){
            loginInfoRet()
        }

        private fun runOnMainThread(block:()->Unit){
            Handler(Looper.getMainLooper()).post {
                block()
            }
        }

        private fun loginInfoRet(){
            runOnMainThread{
                val jsonObject = JSONObject()
                jsonObject.put("packageName", PackageUtil.getPackageName(activity))
                jsonObject.put("channel", PackageUtil.getUmengChannelName(activity))
                jsonObject.put("version", PackageUtil.getPackageVersionName(activity))
                jsonObject.put("token", UserInfoManager.loginToken)
                jsonObject.put("isLogin", false)
                jsonObject.put("isVip", false)
                jsonObject.put("devicePlatform","android")
                jsonObject.put("deviceSystem", Build.VERSION.RELEASE)
                jsonObject.put("deviceBrand", Build.BRAND)
                jsonObject.put("deviceModel", Build.MODEL)
                jsonObject.put("deviceId", DeviceUtil.getAndroidId(activity))

                val str = jsonObject.toString()

                webView!!.evaluateJavascript("javascript:loginInfoRet(" + str + ");", ValueCallback {

                })
            }
        }

        @JavascriptInterface
        fun openLink(uriStr:String){
            runOnMainThread{
                try{
                    val uri = Uri.parse(uriStr)
                    val intent = Intent(Intent.ACTION_VIEW, uri)
                    activity.startActivity(intent)
                }catch (e:Exception){
                    e.printStackTrace()
                }
            }
        }

        @JavascriptInterface
        fun getScreenInfo(){
            screenInfoRet()
        }

        fun screenInfoRet(){
            runOnMainThread{
                val jsonObject = JSONObject()
                jsonObject.put("screenWidth", ScreenUtil.getScreenWidth())
                jsonObject.put("screenHeight", ScreenUtil.getScreenRealHeight())
                jsonObject.put("statusBarHeight",
                    StatusBarUtil.getStatusBarHeight(activity))
                jsonObject.put("bottomBarHeight", StatusBarUtil.getBottomBarHeight())
                jsonObject.put("density", ScreenUtil.getDensity())
                jsonObject.put("densityDpi", ScreenUtil.getDensityDpi())

                val str = jsonObject.toString()

                webView!!.evaluateJavascript("javascript:screenInfoRet(" + str + ");", ValueCallback {

                })
            }
        }

        @JavascriptInterface
        fun showHeaderBar(isShow:Boolean){
            runOnMainThread{
                ViewHelper.setVisibility(isShow,hbv)
            }
        }

        @JavascriptInterface
        fun showHeaderBarBackIcon(isShow:Boolean){
            runOnMainThread{
                hbv!!.showBackIcon(isShow)
            }
        }

        @JavascriptInterface
        fun setHeaderBarStyle(isDark:Boolean){
            runOnMainThread {
                hbv.setStyle(isDark)
            }
        }

        @JavascriptInterface
        fun setHeaderBarBackGroundColor(color:String){
            runOnMainThread{
                hbv.setBackgroundColor(Color.parseColor(color))
            }
        }

        @JavascriptInterface
        fun setHeaderBarTitle(title:String){
            runOnMainThread {
                hbv.setTitle(title)
            }
        }

        @JavascriptInterface
        fun setHeaderBarTitleSize(titleSize:Float){
            runOnMainThread {
                hbv.getTitleView().textSize = titleSize
            }
        }

        @JavascriptInterface
        fun setStatusBarStyle(isDark: Boolean){
            runOnMainThread {
                StatusBarUtil.setStatusBarStyle(activity, isDark)
            }
        }

        @JavascriptInterface
        fun setCanGoBack(canGoBack: Boolean){
            mCanGoBack = canGoBack
        }

    }
}